import { HorizontalBox, CheckBox, ComboBox, Button, VerticalBox, TextEdit, TabWidget, LineEdit } from "std-widgets.slint";
import { UiIo } from "global.slint";
import { CtrtexMView } from "mcu_core.slint";
import { ToolBox } from "tool_box.slint";
import { Memory } from "memory.slint";
import { Peripherals } from "peripherals.slint";
import { AssembleView } from "assemble_view.slint";

export { UiIo }

enum Page {
    Peripherals,
    Memory0,
    Memory1,
    Memory2,
}

component PageButton inherits Rectangle {
    in property <string> text <=> txt.text;
    in property <bool> active;

    callback pressed;

    border-radius: self.height / 4;
    border-color: (touch.has-hover || root.active) ? #456 : #3e3e3e;
    border-width: 1px;
    background: root.active ? #789 : touch.has-hover ? #456 : transparent;
    height: layout.preferred-height * 1.6;
    width: layout.preferred-width + 20px;

    layout := HorizontalLayout {
        spacing: 10px;
        alignment: center;
        txt := Text {
            horizontal-stretch: 1;
            font-size: 14px;
            vertical-alignment: center;
        }

        @children
    }
    touch := TouchArea {
        pointer-event(event) => {
            if (event.kind == PointerEventKind.down) {
                root.pressed();
            }
        }
    }
}

component TabContent inherits VerticalLayout {
    property <Page> active-page: Page.Peripherals;
    horizontal-stretch: 2;

    HorizontalBox {
        alignment: start;

        PageButton {
            text: "Peripherals";
            active: root.active-page == Page.Peripherals;
            pressed => { root.active-page = Page.Peripherals; }
        }
        PageButton {
            text: "Memory0";
            active: root.active-page == Page.Memory0;
            pressed => { root.active-page = Page.Memory0; }
        }
        PageButton {
            text: "Memory1";
            active: root.active-page == Page.Memory1;
            pressed => { root.active-page = Page.Memory1; }
        }
        PageButton {
            text: "Memory2";
            active: root.active-page == Page.Memory2;
            pressed => { root.active-page = Page.Memory2; }
        }
    }

    Rectangle {
        border-color: gray;
        border-width: 1px;
        border-radius: 3px;
        Peripherals {
            visible: active-page == Page.Peripherals;
        }
        Memory {
            memory_idx: 0;
            visible: active-page == Page.Memory0;
        }
        Memory {
            memory_idx: 1;
            visible: active-page == Page.Memory1;
        }
        Memory {
            memory_idx: 2;
            visible: active-page == Page.Memory2;
        }
    }
}


export component AppWindow inherits Window {
    default-font-family: "Consolas";
    default-font-size: 14px;
    title: "MCU Debug Probe";
    //icon: @image-url("icons/app-icon-64.png");

    min-width: 512px;
    min-height: 320px;
    preferred-width: 1200px;
    preferred-height: 600px;

    VerticalLayout {
        padding: 3px;
        
        ToolBox {}

        HorizontalBox {
            padding-top: 3px;
            padding-bottom: 0;
            CtrtexMView {
                horizontal-stretch: 1;
                width: 154px;
            }
            assemble_view:= AssembleView {
                horizontal-stretch: 1;
            }
            TabContent {
                horizontal-stretch: 2;
            }
        }
        HorizontalLayout {
            Text {
                text: UiIo.help_tip;
            }
        }
    }
}
